package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper detailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;
    @Autowired
    private StrategyTagMapper tagMapper;
    @Autowired
    private StrategyCatalogMapper catalogMapper;
    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(detailMapper.selectForList(qo));
    }

    @Override
    public StrategyDetail selectContainContentById(Long id) {
        StrategyDetail detail = detailMapper.selectByPrimaryKey(id);
        StrategyContent content = contentMapper.selectByPrimaryKey(detail.getId());
        detail.setStrategyContent(content);
        return detail;
    }

    @Override
    public AjaxResult saveOrUpdate(StrategyDetail detail, String tags) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            //在insert/update之前,设置一些默认值
            detail.setCreateTime(new Date());
            detail.setSummary("AI算法要收钱,穷鬼");
            //dest页面没有传递,但是可以通过catalog>>dest>>detail
            Destination dest = new Destination();
            StrategyCatalog catalog = catalogMapper.selectByPrimaryKey(detail.getCatalog().getId());
            dest.setId(catalog.getDestId());
            detail.setDest(dest);
            //执行save|| update
            if(detail.getId() != null){
                detailMapper.deleteRelationOfTag(detail.getId());
                detailMapper.updateByPrimaryKey(detail);
                contentMapper.updateByPrimaryKey(detail.getStrategyContent());
            }else{
                detailMapper.insert(detail);
                contentMapper.insert(detail.getId(), detail.getStrategyContent().getContent());
            }
            //中间表维护关系
            String[] arr = tags.split(",");
            for (String tagName : arr) {
                StrategyTag tag = tagMapper.selectByName(tagName);
                detailMapper.insertRelationOfTag(detail.getId(), tag.getId());
            }
        }catch (Exception e){
            ajaxResult.mark(e.getMessage());
        }
        return ajaxResult;
    }

    @Override
    public List<StrategyDetail> selectByDestId(Long id) {
        return detailMapper.selectByDestId(id);
    }

    @Override
    public List<StrategyDetail> selectReplyTop3ByDestId(Long id) {
        PageHelper.startPage(1,3, "sd.replynum DESC");
        return detailMapper.selectByDestId(id);
    }

    @Override
    public List<StrategyDetail> selectAll() {
        return detailMapper.selectAll();
    }

    @Override
    public void updateByStats(StrategyStats stats) {
        detailMapper.updateOfStats(stats);
    }

}
